
#ifndef _TTG_STRUCTURES_BASEDICTIONARY_
#define _TTG_STRUCTURES_BASEDICTIONARY_
#include "Common/Definitions.h"

#include "Objects.h"
#include "EnumerableCollections.h"
#include <list>

namespace ttg
{
	namespace structures
	{
		//Main class. Creates descendant, which is optimized for given size.
		template <class T>
		class Dictionary :public NamedObject,
						  public EnumerableCollection<T *>
		{
			protected:
				inline Dictionary(const char *name)
					:NamedObject(name)
						{ /*nothing*/ }
			public:
				//Adds required object to collection.
				//Optionally, it can be deleted by dictionary destructor.
				virtual bool add(ReleasableWrapper<T> &no) = 0;
				//Adds given NamedObject to collection.
				inline bool add(T *no, bool should_release = false)
				{ return add(ReleasableWrapper<T>(no, should_release)); }
				//Returns first NamedObject with given name or NULL (in case of unexistence)
				virtual T *get(const char *name) = 0;
				//Returns first NamedObject with given name or NULL (in case of unexistence)
				inline T *get(const std::string &name)
				{ return get(name.c_str()); }
				//Returns all contained objects.
				virtual Enumerator<T *> *getAll() = 0;
				//Removes the first NamedObject with given name.
				//In case of success returns removed object, NULL otherwise.
				virtual T *remove(const char *name) = 0;
				//Removes the first NamedObject with given name.
				//In case of success returns removed object, NULL otherwise.
				inline T *remove(const std::string &name)
				{ return remove(name.c_str()); }
				//Removes required NamedObject.
				//In case of success returns removed object, NULL otherwise.
				inline T *remove(NamedObject *no)
				{ return this->remove(no->getName()); }
				//Returns collection with all content and clears dictionary.
				//Note: deleting of all content and collection must be performed by caller.
				virtual EnumerableCollection<T *> *removeAll() = 0;
				//EnumerableCollection member.
				virtual Enumerator<T *> *getEnumerator()
				{ return getAll(); }
				//releases all resources, that are contained in dictionary.
				//Note: "delete" will be called for all contained NamedObjects.
				virtual ~Dictionary()
				{ /*nothing*/ }
			//creates optimized for expected size Dictionary. Never returns NULL.
			static Dictionary<T> *create(size_t expectedsize, const char *name = "default")
			{ return new ttg_internal::structures::ListDictionary<T>(name); }
		};
	}
}

#endif
